package com.atguigu.function;

import com.atguigu.bean.WaterSensor;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.util.Collector;

/**
 * @author yhm
 * @create 2024-04-02 14:06
 */
public class WaterSensorFunction implements FlatMapFunction<String, WaterSensor> {
    @Override
    public void flatMap(String value, Collector<WaterSensor> out) throws Exception {
        try {
            String[] words = value.split(",");
            if (words.length == 3){
                out.collect(new WaterSensor(words[0],Long.valueOf(words[1]),Integer.valueOf(words[2])));
            }else{
                // 也是脏数据
                System.out.println("脏数据为:" + value + ",无法转换为javaBean");
            }
        }catch (Exception e){
            // 处理脏数据
            System.out.println("脏数据为:" + value + ",无法转换为javaBean");
            e.printStackTrace();
        }
    }
}
